package com.spider.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.*;
import java.util.Properties;

//@Component
public class JdbcUtilV8 {
    private static final Logger log = LoggerFactory.getLogger(JdbcUtilV8.class);
    // 目标数据库配置（修改为你的数据库信息）
    private static final String JDBC_URL = "jdbc:mysql://123.57.205.226:33306/gd_pms?useSSL=false&rewriteBatchedStatements=true";
    private static final String USER = "root";
    private static final String PASSWORD = "cBrwByPr3lru1mQ5";

    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver"); // MySQL驱动
            log.info("成功加载 MySQL 驱动");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("加载数据库驱动失败", e);
        }
    }

    // 获取连接
    public static Connection getConnection() throws SQLException {
        Properties props = new Properties();
        props.setProperty("user", USER);
        props.setProperty("password", PASSWORD);
        return DriverManager.getConnection(JDBC_URL, props);
    }

    // 关闭资源
    public static void close(Connection conn, Statement stmt, ResultSet rs) {
        try {
            if (rs != null) rs.close();
            if (stmt != null) stmt.close();
            if (conn != null) conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
